public with sharing class PdfInvoiceController {
 
  private ApexPages.standardController controller {get;set;}
        
  string InvoiceId = ApexPages.currentPage().getParameters().get('id');

  public String pdfName {get;set;}
  private Application__c app = null;
  public DateTime CurrentTime {get;set;}
  
  public PdfInvoiceController(ApexPages.standardController stdController) {
        controller = stdController; 
                app = [select id,name,Company__c,Transaction_Fee__c from Application__c where id =:InvoiceId];
                DateTime todayHour = Datetime.now();
                currentTime = todayHour.addHours(4);  
          }

  public PageReference savePdf() {
 
    PageReference pdf = Page.InvoicePdf;
    // add parent id to the parameters for standardcontroller
    pdf.getParameters().put('id',InvoiceId);
    pdf.getParameters().put('message1','true');  
    pdf.getParameters().put('message2','false');
 
    // create the new attachment
    Attachment attach = new Attachment();
 
    // the contents of the attachment from the pdf
    Blob body;
 
    try {
 
        // returns the output of the page as a PDF
        body = pdf.getContent();
 
    // need to pass unit test -- current bug    
    } catch (VisualforceException e) {
        body = Blob.valueOf('Some Text');
    }
 
    attach.Body = body;
    // add the user entered name
    attach.Name = app.name +' '+ system.Now()+' .pdf';
    attach.IsPrivate = false;
    // attach the pdf to the account
    attach.ParentId = InvoiceId;
    insert attach; 
    
    Receipt__c rec = new Receipt__c();
    rec.Application__c = app.id;
    rec.Company__c = app.Company__c;
    rec.Total_Amount__c = app.Transaction_Fee__c;
    insert rec;
     
   // Redirect the user back to the original page
   PageReference pageRef = new PageReference('/apex/InvoicePdf'); 
   pageRef.getParameters().put('id',InvoiceId);   
   pageRef.getParameters().put('message1','true');  
   pageRef.getParameters().put('message2','false');
   pageRef.setRedirect(true);
   return pageRef; 
  }
 
}